﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using System.Globalization;

namespace QLBDQG
{
    /// <summary>
    /// Interaction logic for QLSAN.xaml
    /// </summary>
    public partial class QLSAN : Window
    {
        private SqlConnection mySqlConnection;
        SqlCommand mySqlCommand;
        SqlDataAdapter mySqlDataAdapter;
        SqlDataReader mySqlDataReader;
        DataSet myDataSet;
        DataTable myDataTable;
        DataRowView rowBeingEdited = null;
        public QLSAN(SqlConnection _SCN)
        {
            InitializeComponent();
            mySqlConnection = _SCN;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            MakeTable();
        }
        public void MakeTable()
        {
            try
            {
                mySqlCommand = new SqlCommand("SHOWSAN", mySqlConnection);
                mySqlCommand.CommandType = CommandType.StoredProcedure;
                mySqlDataAdapter = new SqlDataAdapter();
                mySqlDataAdapter.SelectCommand = mySqlCommand;
                myDataSet = new DataSet();

                mySqlDataAdapter.Fill(myDataSet, "SAN");

                myDataTable = myDataSet.Tables["SAN"];
                dgrSANIF.ItemsSource = myDataTable.DefaultView;
                dgrSANAD.ItemsSource = myDataTable.DefaultView;
                dgrSANED.ItemsSource = myDataTable.DefaultView;
                dgrSANDE.ItemsSource = myDataTable.DefaultView;
                mySqlDataAdapter.Dispose();
            }
            catch (SqlException es)
            {
                MessageBox.Show(es.Message.ToString());
            }
        }
        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void dgrSANIF_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            rowBeingEdited = dgrSANIF.SelectedCells[0].Item as DataRowView;
            txbMaSanIF.Text = rowBeingEdited[0].ToString();
            txbTenSanIF.Text = rowBeingEdited[1].ToString();
            txbDiaChiIF.Text = rowBeingEdited[2].ToString();
        }

        private void dgrSANAD_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void dgrSANAD_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {

        }

        private void dgrSANED_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            try
            {
                rowBeingEdited = dgrSANED.SelectedCells[0].Item as DataRowView;
                txbMaSanED.Text = rowBeingEdited[0].ToString();
                txbTenSanED.Text = rowBeingEdited[1].ToString();
                txbDiaChiED.Text = rowBeingEdited[2].ToString();
            }
            catch (System.Exception ex)
            {
            	
            }
            
        }

        private void dgrSANDE_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            try
            {
                rowBeingEdited = dgrSANDE.SelectedCells[0].Item as DataRowView;
                txbMaSanDE.Text = rowBeingEdited[0].ToString();
                txbTenSanDE.Text = rowBeingEdited[1].ToString();
                txbDiaChiDE.Text = rowBeingEdited[2].ToString();
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void bntAddAD_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                mySqlCommand = new SqlCommand("ADDSAN", mySqlConnection);
                mySqlCommand.CommandType = CommandType.StoredProcedure;
                mySqlCommand.Parameters.Add(new SqlParameter("@TenSan", SqlDbType.VarChar, 50));
                mySqlCommand.Parameters.Add(new SqlParameter("@DiaChi", SqlDbType.VarChar, 50));
                mySqlCommand.Parameters["@TenSan"].Value = txbTenSanAD.Text;
                mySqlCommand.Parameters["@DiaChi"].Value = txbDiaChiAD.Text;
                mySqlDataReader = mySqlCommand.ExecuteReader();
                mySqlDataReader.Dispose();
            }
            catch (SqlException es)
            {
                MessageBox.Show(es.Message.ToString());
            }
            MakeTable();
        }

        private void bntEditED_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                mySqlCommand = new SqlCommand("UPDATESAN", mySqlConnection);
                mySqlCommand.CommandType = CommandType.StoredProcedure;
                mySqlCommand.Parameters.Add(new SqlParameter("@MaSan", SqlDbType.Char, 6));
                mySqlCommand.Parameters.Add(new SqlParameter("@TenSan", SqlDbType.VarChar, 50));
                mySqlCommand.Parameters.Add(new SqlParameter("@DiaChi", SqlDbType.VarChar, 50));
                mySqlCommand.Parameters["@MaSan"].Value = txbMaSanED.Text;
                mySqlCommand.Parameters["@TenSan"].Value = txbTenSanED.Text;
                mySqlCommand.Parameters["@DiaChi"].Value = txbDiaChiED.Text;
                mySqlDataReader = mySqlCommand.ExecuteReader();
                mySqlDataReader.Dispose();
            }
            catch (SqlException es)
            {
                MessageBox.Show(es.Message.ToString());
            }
            MakeTable();
        }

        private void bntDeleteDE_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                mySqlCommand = new SqlCommand("DELETESAN", mySqlConnection);
                mySqlCommand.CommandType = CommandType.StoredProcedure;
                mySqlCommand.Parameters.Add(new SqlParameter("@MaSan", SqlDbType.Char, 6));
                mySqlCommand.Parameters["@MaSan"].Value = txbMaSanDE.Text;
                mySqlDataReader = mySqlCommand.ExecuteReader();
                mySqlDataReader.Dispose();
            }
            catch (SqlException es)
            {
                MessageBox.Show(es.Message.ToString());
            }
            MakeTable();
            txbTenSanDE.Text = null;
            txbDiaChiDE.Text = null;
            txbMaSanDE.Text = null;
        }





    }
    public class San
    {
        public Regex nameEx = new Regex(@"^[A-Za-z ]+$");
        string m_NameOfSan;
        string diachi;
        public string TenSan
        {


            get { return m_NameOfSan; }
            set
            {
                if (value == "")
                    throw new Exception("Ten phai chua it nhat mot ki tu");

                if (!nameEx.Match(value).Success)
                {
                    throw new Exception("Ten khong chua so hay ki tu dac biet");
                }

                m_NameOfSan = value;
            }
        }
        public string DiaChi
        {


            get { return diachi; }
            set
            {
                if (value == "")
                    throw new Exception("Dia chi chua it nhat mot ki tu");

                if (!nameEx.Match(value).Success)
                {
                    throw new Exception("Dia chi khong chua so hay ki tu dac biet");
                }

                diachi = value;
            }
        }

    }
}
